﻿@model SIG.Model.ViewModel.Identity.UserListVM

@{
    var modelName = EntityNames.User;
    ViewBag.PageTitle = modelName;
    ViewBag.Title = ViewBag.PageTitle + "_安全";
}

@section leftNav{
    @Html.Partial("_NavForSecurity")
}



<div class="main-content">

            <div class="panel panel-default">
                <header class="panel-heading">
                    <h3 class="panel-title">

                        <span class="@SettingsManager.User.IconClass"></span> @ViewBag.PageTitle
                    </h3>
                </header>
                <div class="panel-body">
                    <a href="@Url.Action("Create")" class="btn btn-primary pull-right"><span class="glyphicon glyphicon-pencil"></span> @String.Format("新增{0}", EntityNames.User)</a>
                    @using (Html.BeginForm("Index", "User", FormMethod.Get, new { @class = "form-inline" }))
                    {
                        <div class="form-group">
                            <label class="sr-only" for="Username">用户名</label>
                            @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control", placeholder = "用户名" } })
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="StartDate">开始日期</label>
                            @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control", placeholder = "开始日期" } })
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="EndDate">结束日期</label>
                            @Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control", placeholder = "结束日期" } })
                        </div>
                            @*<div class="form-group">
                                    @Html.DropDownList("DepartmentId", null, "--所属部门--", htmlAttributes: new { @class = "form-control" })
                                </div>
                                <div class="form-group">
                                    @Html.DropDownList("PositionId", null, "--所在岗位--", htmlAttributes: new { @class = "form-control" })
                                </div>*@
                            <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> 搜索</button>

                            }
                        </div>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>

                            <th>
                                @Html.DisplayNameFor(model => model.Users.FirstOrDefault().UserName)/@Html.DisplayNameFor(model => model.Users.FirstOrDefault().Email)/@Html.DisplayNameFor(model => model.Users.FirstOrDefault().RealName)
                            </th>                          
                         
                            <th>
                                @Html.DisplayNameFor(model => model.Users.FirstOrDefault().Birthday) / @Html.DisplayNameFor(model => model.Users.FirstOrDefault().GenderName)
                            </th>
                           
                            <th>
                                @Html.DisplayNameFor(model => model.Users.FirstOrDefault().QQ)/@Html.DisplayNameFor(model => model.Users.FirstOrDefault().Mobile)
                            </th>
                                                 
                            <th>
                                @Html.DisplayNameFor(model => model.Users.FirstOrDefault().IsActive)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.Users.FirstOrDefault().CreateDate) /@Html.DisplayNameFor(model => model.Users.FirstOrDefault().LastActivityDate)
                            </th>
                        
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.Users)
            {
                            <tr>

                                <td>
                                    @Html.DisplayFor(modelItem => item.UserName) <br/>@Html.DisplayFor(modelItem => item.Email)<br />
                                     @Html.DisplayFor(modelItem => item.RealName)
                                </td>
                             
                           
                                <td>
                                    @Html.DisplayFor(modelItem => item.Birthday) <br />@Html.DisplayFor(modelItem => item.GenderName)
                                </td>
                              
                                <td>
                                    @Html.DisplayFor(modelItem => item.QQ) <br /> @Html.DisplayFor(modelItem => item.Mobile)
                                </td>
                             
                            
                                <td>
                                    @Html.DisplayFor(modelItem => item.IsActive)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.CreateDate) <br />@Html.DisplayFor(modelItem => item.LastActivityDate)
                                </td>
                           

                                <td>
                                    <a href="@Url.Action("Details", new {id = item.Id})" class="btn btn-info btn-xs" data-toggle="tooltip" data-placement="top" title="查看用户资料">
                                        <span class="glyphicon glyphicon-info-sign"></span>
                                    </a>
                                    <a href="@Url.Action("Edit", new {id = item.Id})" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="top" title="编辑用户资料">
                                        <span class="glyphicon glyphicon-pencil"></span>
                                    </a>
                                    <button data-url="@Url.Action("RestPassword", new {id = item.Id})" class="btn btn-xs btn-default rest-pwd" data-id="@Html.DisplayFor(modelItem => item.Id)"
                                            type="button" data-toggle="modal" data-target="#myModal" rel="tooltip" data-placement="top" title="重设密码">
                                        <span class="glyphicon glyphicon-repeat"></span>
                                    </button>

                                    @{
                                        var tooltip = item.IsActive ? "锁定" : "激活";
                                        tooltip = tooltip + EntityNames.User;
                                    }

                                    <button data-url="@Url.Action("IsActive", new {id = item.Id})" class="btn btn-xs btn-default active-item" type="button" data-toggle="tooltip" data-placement="top" title="@tooltip">
                                        <span class="glyphicon glyphicon-lock"></span>
                                    </button>
                                    <button data-url="@Url.Action("Delete", new {id = item.Id})" class="btn btn-xs btn-danger delete-item" type="button" data-toggle="tooltip" data-placement="top" title="删除用户">
                                        <span class="glyphicon glyphicon-trash"></span>
                                    </button>
                                </td>
                            </tr>
                                        }
                    </tbody>
                </table>
                <div class="panel-footer text-center">
                    @Html.PagedListPager((IPagedList)ViewBag.OnePageOfUsers, page => Url.Action("Index",
           new { page, username = Model.StartDate, startDate = Model.StartDate, endDate = Model.EndDate}))
                </div>
            </div>
     
</div>



@Html.Partial("_SetPassword", Model.SetPasswordVM)

@section Footer {

    @Scripts.Render("~/bundles/jqueryval")


    <script>

        function SetPasswordSuccess(data) {
            if (data) {
                toastr.success('密码已重设成功！', '密码重设');
                $("#UserId,#NewPassword,#ConfirmPassword").val("");
                $('#myModal').modal('hide');
            } else {
                toastr.error('密码重设失败！', '密码重设');
            }

        }


        $(function () {
            var modelName = "@EntityNames.User";
            //当前链接;
            Common.ActiveCurrentNav();

            //确认框默认语言
            bootbox.setDefaults({
                locale: "zh_CN"
            });

            //删除数据
            $('.delete-item').click(function (e) {
                e.preventDefault();

                var url = $(this).attr('data-url');
                var that = $(this);
                bootbox.confirm("你确定要删除此项吗？", function (result) {
                    /*DELETE*/
                    if (result) {
                        $.ajax({
                            url: url,
                            dataType: "json",
                            type: "POST",
                            contentType: 'application/json; charset=utf-8',
                            data: JSON.stringify({}),
                            async: true,
                            processData: false,
                            cache: false,
                            success: function (data) {

                                if (data) {

                                    toastr.success(modelName + '已删除成功！', '删除' + modelName);
                                    that.closest('tr').remove();
                                } else {
                                    toastr.error(modelName + '删除失败！', '删除' + modelName);
                                }
                            },
                            error: function (xhr) {
                                toastr.error(modelName + '删除失败！', '删除' + modelName);
                            }
                        });
                    }

                });


            });


            //锁定 激活
            $('.active-item').click(function (e) {
                e.preventDefault();

                var actionName = $(this).attr('data-original-title');

                var url = $(this).attr('data-url');
                var that = $(this);

                        $.ajax({
                            url: url,
                            dataType: "json",
                            type: "POST",
                            contentType: 'application/json; charset=utf-8',
                            data: JSON.stringify({}),
                            async: true,
                            processData: false,
                            cache: false,
                            success: function (data) {
                                if (data) {
                                    toastr.success(actionName + '成功！', actionName);
                                    var inputCheck = that.closest('tr').find("input[type=checkbox]");

                                    if (inputCheck.is(":checked")) {
                                        inputCheck.prop('checked', '')
                                        that.attr('data-original-title', '激活' + modelName)
                                    } else {
                                        inputCheck.prop('checked', "checked")
                                        that.attr('data-original-title', '锁定' + modelName)
                                    }
                                } else {
                                    toastr.error(actionName + '失败！', actionName);
                                }
                            },
                            error: function (xhr) {
                                toastr.error(actionName + '失败！', actionName);
                            }
                        });

            });


            $('#myModal').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget);
                var userId = button.data('id');
                var modal = $(this);
                modal.find('#UserId').val(userId);
            });

        });
    </script>
}